//=============================================================================

#include <windows.h>
#include "../inc/aFilesWomb.h"

//=============================================================================

aFilesWomb::aFilesWomb()
{
  const DWORD bufferSize = 4096;
  
  wchar_t tmpBuffer[bufferSize] = {0};

  GetCurrentDirectory(bufferSize, tmpBuffer);
  defaultPath = tmpBuffer;
  dataPath = defaultPath + "\\source";
  worldsPath = dataPath + "\\worlds";
  charactersPath = dataPath + "\\characters";
}

//=============================================================================

afbRet aFilesWomb::afWombIni()
{
  afbRet r = afGetAllWorlds();

  if (r != FWO_DONE)
    return r;

  r = afGetAllCharacters();

  return r;
}

//=============================================================================

void aFilesWomb::afGetWorlds(aList<aString>& list)
{
  if (worlds.alGetCount() <= 0)
    return;

  aItem<aString>* tmpString = worlds.alGetFirstItem();

  while(tmpString)
  {
    list.alAddItem()->data = tmpString->data;

    tmpString = tmpString->next;
  }
}

//=============================================================================

void aFilesWomb::afGetCharacters(aList<aString>& list)
{
  if (characters.alGetCount() <= 0)
    return;

  aItem<aString>* tmpString = characters.alGetFirstItem();

  while(tmpString)
  {
    list.alAddItem()->data = tmpString->data;

    tmpString = tmpString->next;
  }
}

//=============================================================================

afbRet aFilesWomb::afGetAllWorlds()
{
  worlds.alRemoveItems();

  WIN32_FIND_DATA fnd;
  HANDLE found;
  aString allPath = worldsPath + "\\*.*";
  aString notThis = "..";
  aString tmpString;
  found = FindFirstFile(allPath.asGetString(), &fnd);

  if (found == INVALID_HANDLE_VALUE)
    return FWO_INVALID_DEFAULT_PATH;

  while(FindNextFile(found, &fnd))
  {
    if (notThis == fnd.cFileName)
      continue;

    tmpString = fnd.cFileName;
    if (tmpString.asFindAfter('.', 0).asGetLength() > 0)//if it's not folder
      worlds.alAddItem()->data = fnd.cFileName;
  }

  return FWO_DONE;
}

//=============================================================================

afbRet aFilesWomb::afGetAllCharacters()
{
  characters.alRemoveItems();

  WIN32_FIND_DATA fnd;
  HANDLE found;
  aString allPath = charactersPath + "\\*.*";
  aString notThis = "..";
  aString tmpString;
  found = FindFirstFile(allPath.asGetString(), &fnd);

  if (found == INVALID_HANDLE_VALUE)
    return FWO_INVALID_DEFAULT_PATH;

  while(FindNextFile(found, &fnd))
  {
    if (notThis == fnd.cFileName)
      continue;

    tmpString = fnd.cFileName;
    if (tmpString.asFindAfter('.', 0).asGetLength() > 0)//if it's not folder
      characters.alAddItem()->data = fnd.cFileName;
  }

  return FWO_DONE;
}

//=============================================================================

aFilesWomb::~aFilesWomb()
{
}

//=============================================================================
